package com.xc.ksxt.controller;

import com.xc.common.object.LayuiTableResponse;
import com.xc.common.object.PageInfo;
import com.xc.common.object.ResponseEntity;
import com.xc.common.tool.Helper;
import com.xc.db.tables.Symbol;
import com.xc.ksxt.entity.SymbolEntity;
import com.xc.ksxt.entity.QuestionEntity;
import com.xc.ksxt.service.SymbolService;
import com.xc.ksxt.vo.SymbolVO;
import com.xc.sys.service.SysUserService;
import com.xc.sys.vo.SysUserVO;
import org.apache.commons.lang3.StringUtils;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import static org.jooq.impl.DSL.*;
@RestController
@RequestMapping("/symbol")
@CrossOrigin
public class SymbolController {
    @Autowired
    private DSLContext dsl;
    @Autowired
    private SymbolService symbolService;
    private Symbol SYMBOLTB = Symbol.SYMBOL.as("SYMBOLTB");
    @CrossOrigin(origins = "*", allowedHeaders = {"*"}, methods = {RequestMethod.GET})
    @RequestMapping({"/getPageList"})
    public LayuiTableResponse<SymbolVO> getPageList(HttpServletRequest request) {
        LayuiTableResponse<SymbolVO> ltr = new LayuiTableResponse<>();
        Condition cond = DSL.trueCondition();
        String code = request.getParameter("code");
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        Integer page = Integer.valueOf(request.getParameter("page"));
        if (StringUtils.isNotBlank(code)) {
            cond = cond.and(SYMBOLTB.CODE.like("%"+code+"%"));
        }
        Long count = symbolService.count(cond);
        ltr.setCount(count);
        PageInfo<SymbolEntity> pageInfo = new PageInfo<>(limit, page);
        Result<Record> list = symbolService.select(cond, pageInfo,SYMBOLTB.CODE.asc(),SYMBOLTB.ID.desc());
        List<SymbolVO> symbolEntityList = list.into(SymbolVO.class);
        for (SymbolVO symbolEntity : symbolEntityList){
            setTxt(symbolEntity);
        }
        ltr.setData(symbolEntityList);
        return ltr;
    }

    private void setTxt(SymbolVO symbolEntity) {
    }

    @RequestMapping({"/getById"})
    public SymbolVO getById(Integer id) {
        Record r = symbolService.getRecordById(id);
        SymbolVO entity = r.into(SymbolVO.class);
        setTxt(entity);
        return entity;
    }
    @RequestMapping({"/save"})
    public ResponseEntity<SymbolEntity> save(@RequestBody HashMap<String,String> params, HttpServletRequest request) {
        ResponseEntity<SymbolEntity> res = new ResponseEntity<>();
        SymbolEntity symbolEntity = Helper.map2Bean(new SymbolEntity(), params);
        symbolService.save(symbolEntity);
        return res;
    }
    @RequestMapping({"/delete"})
    public LayuiTableResponse<SymbolEntity> delete(@RequestParam("ids[]") Integer[] ids) {
        LayuiTableResponse<SymbolEntity> ltr = new LayuiTableResponse<>();
        for (Integer id : ids) {
            SymbolEntity m = symbolService.getEntityById(id);
            symbolService.deleteById(id);
        }
        return ltr;
    }
}
